package com.qk.management.controller;

import com.qk.common.Result;

import com.qk.domain.customer.CustomerDO;
import com.qk.dto.customer.CustomerAddDTO;
import com.qk.dto.customer.CustomerListDTO;
import com.qk.dto.customer.CustomerUpdateDTO;
import com.qk.entity.Customer;
import com.qk.management.aop.anno.LogAnno;
import com.qk.management.service.CustomerService;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 客户管理Controller
 */
@Slf4j
@RestController
@RequestMapping("/customers")
@SuppressWarnings("all")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    /**
     * 客户列表查询
     * @param dto 查询条件
     * @return 分页结果
     */
  @LogAnno
    @GetMapping
    public Result getCustomerList(CustomerListDTO dto) {
        log.info("客户列表查询，参数: {}", dto);
        return Result.success(customerService.getCustomerList(dto));
    }

    /**
     * 根据ID查询客户
     * @param id 客户ID
     * @return 客户信息
     */
    @LogAnno
    @GetMapping("/{id}")
    public Result getCustomerById(@PathVariable Integer id) {
        log.info("根据ID查询客户，ID: {}", id);
        if (Objects.isNull(id)) {
            return Result.error("参数错误");
        }
        Customer customer = customerService.getById(id);
        if (Objects.isNull(customer)) {
            return Result.error("客户不存在");
        }
        return Result.success(customer);
    }
    
    /**
     * 添加客户
     * @param dto 客户信息
     * @return 添加结果
     */
    @LogAnno
    @PostMapping
    public Result addCustomer(@RequestBody CustomerAddDTO dto) {
        customerService.addCustomer(dto);
        return Result.success();
    }
    /**
     * 修改客户
     * @param dto 客户信息
     * @return 修改结果
     */
    @LogAnno
    @PutMapping
    public Result updateCustomer(@RequestBody CustomerUpdateDTO dto) {
        log.info("修改客户，参数: {}", dto);
        customerService.updateCustomer(dto);
        return Result.success();
    }
}